package com.tiyende.kash.loop.ui.dialog

import android.content.Context
import android.text.SpannableString
import android.view.LayoutInflater
import android.view.View
import com.tiyende.kash.loop.common.CommonDialog
import com.tiyende.kash.loop.databinding.TiyendeDialogTipsBinding
import com.tiyende.kash.loop.event.TiyendeEventPointType

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：提示dialog
 */
class TiyendeTipsDialog: CommonDialog {

    private lateinit var binding: TiyendeDialogTipsBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark: String? = ""

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String) : super(context) {
        eventRemark = remark
    }
    constructor(context: Context, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getTiyendeRootView(): View {
        binding = TiyendeDialogTipsBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initTiyendeView() {
    }

    override fun initTiyendeListener() {
        setOnClickListener(binding.imageViewTipsClose)//关闭
        setOnClickListener(binding.btTipsCancel) //取消
        setOnClickListener(binding.btTipsOK) //ok
    }

    override fun loadTiyendeData() {
    }

    override fun onTiyendeViewClick(v: View?) {
        dismiss()
        mCallback?.onClick(v)
    }

    /**
     * 设置埋点备注
     */
    fun addTiyendeTipsEventRemark(remark: String?){
        eventRemark = remark
    }

    /**
     * 设置提示图标
     */
    fun addTiyendeTipsIconResource(resId: Int){
        binding.tipsIcon.setImageResource(resId)
    }

    /**
     * 设置提示title
     */
    fun addTiyendeTitleText(title: String){
        binding.txtTipsTitle.text = title
    }

    /**
     * 设置提示title
     */
    fun addTiyendeTitleText(title: String, color: Int){
        addTiyendeTitleText(title)
        binding.txtTipsTitle.setTextColor(color)
    }

    /**
     * 设置提示语
     */
    fun addTiyendeTipsText(tips: String){
        binding.txtTipsMsg.text = tips
    }

    /**
     * 设置提示语
     */
    fun addTiyendeTipsText(spannableString: SpannableString){
        binding.txtTipsMsg.text = spannableString
    }

    /**
     * 设置提示语对齐方式
     */
    fun addTiyendeTipsGravity(gravity: Int){
        binding.txtTipsMsg.gravity = gravity
    }

    /**
     * 设置显示一个按钮或两个按钮
     */
    fun isTiyendeDoubleButton(isDouble: Boolean){
        if (isDouble){//显示两个按钮
            binding.btTipsCancel.visibility = View.VISIBLE
            binding.btTipsOK.visibility = View.VISIBLE
        } else {
            binding.btTipsCancel.visibility = View.GONE
            binding.btTipsOK.visibility = View.VISIBLE
        }
    }

    /**
     * 设置取消文字
     */
    fun addTiyendeBtnCancelText(text: String){
        binding.btTipsCancel.text = text
    }

    /**
     * 设置取消按钮背景
     */
    fun addTiyendeBtnCanceBackgroundResource(color: Int){
        binding.btTipsCancel.shapeDrawableBuilder.setSolidColor(color).intoBackground()
    }

    /**
     * 设置ok文字
     */
    fun addTiyendeBtnOkText(text: String){
        binding.btTipsOK.text = text
    }

    /**
     * 设置ok按钮背景
     */
    fun addTiyendeBtnOkBackgroundResource(color: Int){
        binding.btTipsOK.shapeDrawableBuilder.setSolidColor(color).intoBackground()
    }

    /**
     * 设置两个按钮背景
     */
    fun addTiyendeButtonDoubleBackgroundResource(cancelColor: Int, okColor: Int){
        binding.btTipsCancel.shapeDrawableBuilder.setSolidColor(cancelColor).intoBackground()
        binding.btTipsOK.shapeDrawableBuilder.setSolidColor(okColor).intoBackground()
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_TIPS_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_TIPS_DISMISS,eventRemark)
    }
}